//
//  LXChooseManagerViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/27.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXChooseManagerViewController.h"
#import "FuZeRenListCell.h"
#import "FuZeRenModel.h"

@interface LXChooseManagerViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic) UITableView       *tableView;
@property (nonatomic) NSMutableArray    *data;
@property (nonatomic) FuZeRenModel    *fuZeModel;
@property (nonatomic) FuZeRenListCell *cell;
@property (nonatomic) BOOL isClickBtn;// 是否点击过提交按钮

@end

@implementation LXChooseManagerViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self layoutCustomNav:self.view title:@"选择负责人"];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([FuZeRenListCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([FuZeRenListCell class])];
    
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       [self loadData];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    [self setBottomViewWithText:@"提交"];
    
}


- (void)loadData{

    NSString *url = [NSString stringWithFormat:@"%@",GetResponsible_URL];
    
   
        
    [MSNetworkManager postRevertUrl:url params:@{} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        [self endRefreshing];
        
        if ([self.tableView.mj_header isRefreshing]) {
            [self.data removeAllObjects];
        }
        self.data = [FuZeRenModel mj_objectArrayWithKeyValuesArray:resonseObj];
        [self.tableView reloadData];
            
        [self isHaveEmptyData];
            
       
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self endRefreshing];
        [self isHaveEmptyData];
    } withShowHud:YES];

}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    FuZeRenListCell *cell = [tableView dequeueReusableCellWithIdentifier:[FuZeRenListCell reuseIdentifier]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    FuZeRenModel *model = self.data[indexPath.row];
   
    cell.nameLb.text = model.realName;
    cell.roleNameLb.text = model.roleName;
    cell.userNameLb.text = @"";
    
    // 不为空,说明需要选中带入的数据
    if (!ObjectIsEmpty(self.model)) {
        if([model.ID  isEqualToString:self.model.ID]){
            cell.iconImage.image =  Image(@"small_select_icon");
            self.cell = cell;
            self.fuZeModel = self.model;
        }
    }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FuZeRenListCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    self.cell.iconImage.image = Image(@"small_unSelect_icon");
    
    cell.iconImage.image =  Image(@"small_select_icon");
    self.fuZeModel = self.data[indexPath.row];
    self.cell = cell;
    
}
#pragma mark ---- 提交点击事件
- (void)clickBottomView:(UIButton *)sender{
    
    
    if (self.ToSendValue) {
        
        
        if (ObjectIsEmpty(self.fuZeModel)) {
            showAlert(@"请选择负责人");
            return;
        }
        
        self.isClickBtn = YES;
        self.ToSendValue(self.fuZeModel);
        [self.navigationController popViewControllerAnimated:YES];

    }
    
    
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight, ScreenWidth, ScreenHeight - LL_StatusBarAndNavigationBarHeight - kComHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = kComHeight;
              _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}
- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }

}
- (void)isHaveEmptyData{
    if (self.data.count == 0) {
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tipImage = [UIImage imageNamed:@"No_order_icon"];
        self.tableView.tipView.hidden = NO;
        self.tableView.scrollEnabled = YES;
    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}
@end
